package com.shrct.businessprocess.purchase.impl;

import com.google.common.collect.Iterables;
import com.shrct.businessprocess.budget.RCCost4ACTItem;
import com.shrct.businessprocess.budget.RCCostBreakDown;
import com.shrct.businessprocess.budget.RCDirectCostRequest;
import com.shrct.businessprocess.budget.RCHasCostBudgetCostRequest;
import com.shrct.businessprocess.product.RCBrand;
import com.shrct.businessprocess.product.RCProductServiceTracer;
import com.shrct.businessprocess.product.impl.RCBrandImpl;
import com.shrct.businessprocess.project.RCExchangeRate;
import com.shrct.businessprocess.project.RCProjectMoney;
import com.shrct.businessprocess.purchase.RCBasePurchaseOrder;
import com.shrct.businessprocess.purchase.RCBasePurchaseRequest;
import com.shrct.businessprocess.purchase.RCFullServicePurchaseBudget;
import com.shrct.businessprocess.purchase.RCHasPurchaseBudget;
import com.shrct.businessprocess.purchase.RCProductPurchaseRequest;
import com.shrct.businessprocess.purchase.RCServiceProductPurchaseRequest;
import com.shrct.businessprocess.purchase.impl.RCHasPurchaseBudgetImpl;
import com.shrct.businessprocess.purchase.impl.RCServiceProductPurchaseRequestImpl;
import com.shrct.businessprocess.purchase.proxy.RCFullServicePurchaseBudgetProxy;
import edu.fudan.mylang.pf.IGenericQuery;
import edu.fudan.mylang.pf.IObjectFactory;
import edu.fudan.mylang.utils.DateUtil;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import javax.persistence.AssociationOverride;
import javax.persistence.AssociationOverrides;
import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import org.eclipse.xtext.xbase.lib.CollectionLiterals;
import org.eclipse.xtext.xbase.lib.Functions.Function1;
import org.eclipse.xtext.xbase.lib.IterableExtensions;
import org.eclipse.xtext.xbase.lib.ObjectExtensions;
import org.eclipse.xtext.xbase.lib.Procedures.Procedure1;

@Entity
@DiscriminatorValue("RCFULLSERVICEPURCHASEBUDGET_TYPE")
public class RCFullServicePurchaseBudgetImpl extends RCHasPurchaseBudgetImpl implements RCFullServicePurchaseBudget {
  public RCFullServicePurchaseBudgetImpl() {
    super();
  }
  
  public RCFullServicePurchaseBudgetImpl(final RCCostBreakDown cbd) {
    super( cbd);
    
  }
  
  @JoinColumn(name = "productCategory_id")
  @ManyToOne(targetEntity = RCBrandImpl.class, cascade = { CascadeType.MERGE, CascadeType.PERSIST })
  private RCBrand productCategory;
  
  public RCBrand getProductCategory() {
    return this.productCategory;
  }
  
  public RCFullServicePurchaseBudget setProductCategory(final RCBrand productCategory) {
    this.productCategory = productCategory;
    return this;			
    
  }
  
  @Embedded
  @AttributeOverrides(value = { @AttributeOverride(name = "amount", column = @Column(name = "contractPrice_amount") ), @AttributeOverride(name = "workaround", column = @Column(name = "contractPrice_workaround") ) } )
  @AssociationOverrides(value = @AssociationOverride(name = "exchange", joinColumns = @JoinColumn(name = "contractPrice_exchange") ) )
  private RCProjectMoney contractPrice = com.shrct.businessprocess.project.RCProjectMoney.create();
  
  public RCProjectMoney getContractPrice() {
    return this.contractPrice;
  }
  
  public RCFullServicePurchaseBudget setContractPrice(final RCProjectMoney contractPrice) {
    this.contractPrice = contractPrice;
    return this;			
    
  }
  
  private Double contractTaxRate;
  
  public Double getContractTaxRate() {
    return this.contractTaxRate;
  }
  
  public RCFullServicePurchaseBudget setContractTaxRate(final Double contractTaxRate) {
    this.contractTaxRate = contractTaxRate;
    return this;			
    
  }
  
  @Embedded
  @AttributeOverrides(value = { @AttributeOverride(name = "amount", column = @Column(name = "netPrice_amount") ), @AttributeOverride(name = "workaround", column = @Column(name = "netPrice_workaround") ) } )
  @AssociationOverrides(value = @AssociationOverride(name = "exchange", joinColumns = @JoinColumn(name = "netPrice_exchange") ) )
  private RCProjectMoney netPrice = com.shrct.businessprocess.project.RCProjectMoney.create();
  
  public RCProjectMoney getNetPrice() {
    return this.netPrice;
  }
  
  public RCFullServicePurchaseBudget setNetPrice(final RCProjectMoney netPrice) {
    this.netPrice = netPrice;
    return this;			
    
  }
  
  private Double netTaxRate;
  
  public Double getNetTaxRate() {
    return this.netTaxRate;
  }
  
  public RCFullServicePurchaseBudget setNetTaxRate(final Double netTaxRate) {
    this.netTaxRate = netTaxRate;
    return this;			
    
  }
  
  public String getTypeId() {
    return "RCC1Budget";
  }
  
  public RCExchangeRate getDefaultPurchaseExchangeRate() {
    RCExchangeRate _exchange = this.netPrice.getExchange();
    return _exchange;
  }
  
  public void purchaseRequestAuditPassed(final RCProductPurchaseRequest purchaseRequest) {
  }
  
  public String getSortKey() {
    String _typeId = this.getTypeId();
    String _codeid = this.productCategory.getCodeid();
    String _plus = (_typeId + _codeid);
    return _plus;
  }
  
  public String getName() {
    String _name = this.productCategory==null?(String)null:this.productCategory.getName();
    return _name;
  }
  
  public String getEntityName() {
    String _name = this.getName();
    return _name;
  }
  
  public RCServiceProductPurchaseRequest createServiceProductPurchaseRequest() {RCServiceProductPurchaseRequest rCServiceProductPurchaseRequest=RCServiceProductPurchaseRequestImpl.create(this,getObjectFactory());
    final Procedure1<RCServiceProductPurchaseRequest> _function = new Procedure1<RCServiceProductPurchaseRequest>() {
        public void apply(final RCServiceProductPurchaseRequest it) {
          Date _day = DateUtil.today();
          it.setDate(_day);
        }
      };
    RCServiceProductPurchaseRequest _doubleArrow = ObjectExtensions.<RCServiceProductPurchaseRequest>operator_doubleArrow(rCServiceProductPurchaseRequest, _function);
    return _doubleArrow;
  }
  
  public Iterable<RCServiceProductPurchaseRequest> getServicePurchaseRequests() {
    Collection<RCBasePurchaseRequest> _purchaseRequests = this.getPurchaseRequests();
    Iterable<RCServiceProductPurchaseRequest> _filter = Iterables.<RCServiceProductPurchaseRequest>filter(_purchaseRequests, RCServiceProductPurchaseRequest.class);
    return _filter;
  }
  
  public Iterable<RCProductServiceTracer> getRCProductServiceTracer() {
    IGenericQuery<RCProductServiceTracer> query=getObjectFactory().createGenericQuery(RCProductServiceTracer.class,"select e from com.shrct.businessprocess.product.impl.RCProductServiceTracerImpl as e   where ((e.budgetContract.budget) = (:this)) and  e.active = 1      ").setParameter("this",this);
    Collection<RCProductServiceTracer> _list = query.list();
    return _list;
  }
  
  public RCHasPurchaseBudget asBudget() {
    return this;
  }
  
  public Iterable<RCCost4ACTItem> getCost4ACItems() {
    Iterable<RCCost4ACTItem> _xblockexpression = null;
    {
      Collection<RCHasCostBudgetCostRequest> _costRequests = this.getCostRequests();
      Iterable<RCDirectCostRequest> _filter = Iterables.<RCDirectCostRequest>filter(_costRequests, RCDirectCostRequest.class);
      final Function1<RCDirectCostRequest,Iterable<RCCost4ACTItem>> _function = new Function1<RCDirectCostRequest,Iterable<RCCost4ACTItem>>() {
          public Iterable<RCCost4ACTItem> apply(final RCDirectCostRequest it) {
            Iterable<RCCost4ACTItem> _cost4ACItems = it.getCost4ACItems();
            return _cost4ACItems;
          }
        };
      Iterable<Iterable<RCCost4ACTItem>> _map = IterableExtensions.<RCDirectCostRequest, Iterable<RCCost4ACTItem>>map(_filter, _function);
      final Iterable<RCCost4ACTItem> directCosts = Iterables.<RCCost4ACTItem>concat(_map);
      Collection<RCBasePurchaseRequest> _purchaseRequests = this.getPurchaseRequests();
      final Function1<RCBasePurchaseRequest,Boolean> _function_1 = new Function1<RCBasePurchaseRequest,Boolean>() {
          public Boolean apply(final RCBasePurchaseRequest it) {
            Boolean _isPassed = it.getIsPassed();
            return _isPassed;
          }
        };
      Iterable<RCBasePurchaseRequest> _filter_1 = IterableExtensions.<RCBasePurchaseRequest>filter(_purchaseRequests, _function_1);
      final Function1<RCBasePurchaseRequest,Collection<? extends RCBasePurchaseOrder>> _function_2 = new Function1<RCBasePurchaseRequest,Collection<? extends RCBasePurchaseOrder>>() {
          public Collection<? extends RCBasePurchaseOrder> apply(final RCBasePurchaseRequest it) {
            Collection<? extends RCBasePurchaseOrder> _purchaseOrders = it.getPurchaseOrders();
            return _purchaseOrders;
          }
        };
      Iterable<Collection<? extends RCBasePurchaseOrder>> _map_1 = IterableExtensions.<RCBasePurchaseRequest, Collection<? extends RCBasePurchaseOrder>>map(_filter_1, _function_2);
      Iterable<? extends RCBasePurchaseOrder> _flatten = Iterables.concat(_map_1);
      final Function1<RCBasePurchaseOrder,Boolean> _function_3 = new Function1<RCBasePurchaseOrder,Boolean>() {
          public Boolean apply(final RCBasePurchaseOrder it) {
            Boolean _isInitialized = it.getIsInitialized();
            return _isInitialized;
          }
        };
      Iterable<? extends RCBasePurchaseOrder> orders = IterableExtensions.filter(_flatten, _function_3);
      final Function1<RCBasePurchaseOrder,Iterable<RCCost4ACTItem>> _function_4 = new Function1<RCBasePurchaseOrder,Iterable<RCCost4ACTItem>>() {
          public Iterable<RCCost4ACTItem> apply(final RCBasePurchaseOrder it) {
            Iterable<RCCost4ACTItem> _cost4ACItems = it.getCost4ACItems();
            return _cost4ACItems;
          }
        };
      Iterable<Iterable<RCCost4ACTItem>> _map_2 = IterableExtensions.map(orders, _function_4);
      final Iterable<RCCost4ACTItem> c1 = Iterables.<RCCost4ACTItem>concat(_map_2);
      ArrayList<Iterable<RCCost4ACTItem>> _newArrayList = CollectionLiterals.<Iterable<RCCost4ACTItem>>newArrayList(c1, directCosts);
      Iterable<RCCost4ACTItem> _flatten_1 = Iterables.<RCCost4ACTItem>concat(_newArrayList);
      final Procedure1<Iterable<RCCost4ACTItem>> _function_5 = new Procedure1<Iterable<RCCost4ACTItem>>() {
          public void apply(final Iterable<RCCost4ACTItem> it) {
            final Procedure1<RCCost4ACTItem> _function = new Procedure1<RCCost4ACTItem>() {
                public void apply(final RCCost4ACTItem it) {
                  it.setBudget(RCFullServicePurchaseBudgetImpl.this);
                }
              };
            IterableExtensions.<RCCost4ACTItem>forEach(it, _function);
          }
        };
      Iterable<RCCost4ACTItem> _doubleArrow = ObjectExtensions.<Iterable<RCCost4ACTItem>>operator_doubleArrow(_flatten_1, _function_5);
      _xblockexpression = (_doubleArrow);
    }
    return _xblockexpression;
  }
  
  public static RCFullServicePurchaseBudget create(final RCCostBreakDown cbd, final IObjectFactory objectFactory) {
    com.shrct.businessprocess.purchase.RCFullServicePurchaseBudget rCFullServicePurchaseBudget = new com.shrct.businessprocess.purchase.impl.RCFullServicePurchaseBudgetImpl(
    	cbd
    );
    objectFactory.create(rCFullServicePurchaseBudget);
    return rCFullServicePurchaseBudget;			
    
  }
  
  public static RCFullServicePurchaseBudget createRCFullServicePurchaseBudgetImpl(final RCCostBreakDown cbd, final IObjectFactory objectFactory, final Procedure1<RCFullServicePurchaseBudget> init) {
    com.shrct.businessprocess.purchase.RCFullServicePurchaseBudget rCFullServicePurchaseBudget = new com.shrct.businessprocess.purchase.impl.RCFullServicePurchaseBudgetImpl(
    	cbd
    );
    objectFactory.create(rCFullServicePurchaseBudget);
    init.apply(rCFullServicePurchaseBudget);
    
    return rCFullServicePurchaseBudget;			
    
  }
  
  public RCFullServicePurchaseBudgetProxy toProxy() {
    com.shrct.businessprocess.purchase.proxy.RCFullServicePurchaseBudgetProxy proxy = new com.shrct.businessprocess.purchase.proxy.RCFullServicePurchaseBudgetProxy();
    proxy.setId(this.getId());
    if(this instanceof edu.fudan.mylang.pf.INamedModelObject){
    	proxy.setName(((edu.fudan.mylang.pf.INamedModelObject)this).getEntityName());
    }
    return proxy;
    
  }
}
